'use strict';

$(function(){
    $(".diaorder_title").html("数据加载中····")
    dialog("#d1");

    var id = GetQueryString("id");
    var status = GetQueryString("status")
    $.ajax({
        type:"post",
        url:url+'/trade/orderdetails.html?token='+token,
        async:true,
        data:{'token':token,"id":id},
        dataType:'json',
        success:function(result){
            // console.log(result)
            if(result.status === "0001"){
                var data = result.data;
                type = 2;
                //买入/卖出状态
                var type = data.type;
                var buy = type == 1 ? "买入" : "卖出";
                var buy2 = type == 1 ? "卖方" : "买方";
                var total_html =type == 1 ? "实收" : "实付";
                if(type === 2 && data.status == 1){
                    $(".orderdetails_picd").remove();
                }
                $(".buy").html(buy);
                $(".buy2").html(buy2);
                $("#total_html").html(total_html);
                $("#total").html(data.total + "SDC"); //实收/实付
                $("#rmb").html(data.rmb + "CNY"); //人民币
                //订单类型
                if(status == 1){
                    $("#status").html("进行中");
                }else if(status == 2){
                    $("#status").html("申诉中");
                }
                $("#number").html(data.number + "SDC");    //数量
                $("#sysprice").html(data.sysprice + "USD");    //单价
                $("#amount").html(data.amount + "USD");    //总价
                // $("#sysprice").html(data.sysprice + "USD");    //单价
                if(type === 2){
                    var html = '<p class="order_title">手续费<span class="order_titlesp on2">' + data.terrace_fee +'</span></p>'//手续费
                    $("#zhongjia").append(html);
                }

                $("#created_at").html(data.created_at);    //创建时间
                //是否有上传交易凭证（0：待上传 1：已上传）
                var picture = data.picture === 0 ? "待上传": "已上传";
                $("#picture").html(picture)
                //是否确认（0：未确认 1：已确认）
                var confirm = data.confirm === 0 ? "未确认": "已确认";
                $("#confirm").html(confirm);
                $("#realname").html(data.realname);    //户名
                $("#alipay").html(data.alipay);    //支付宝
                $("#phone").html(data.phone);    //联系电话
                //交易凭证路径
                var url1 = data.url != "" ? data.url : "../images/orderdetails_pic.png";
                $("#headImg").attr("src",url1);
                // $("#headImg").attr("data-url",url1);

                // 是否需要上传凭证（0：需要 1：不需要)
                if(data.upload === 0){
                    var url_html = "../images/orderdetails_pic.png";
                    $(".orderdetails_pic").attr("src",url_html);
                }else{
                    if(data.url == ""){
                        $(".orderdetails_picd").remove();
                    }else{
                        $("#fileInput").remove();
                        var url_html = `<img src="${url1}" alt="" class="orderdetails_pic" id="headImg"/>`;
                        
                        $("#form1").html(url_html);
                        $("#headImg").on("click",function(){
                            var dia_url = $(this).attr("src");
                            $(".dialog_img").attr("src",dia_url);
                            dialog('#d2');
                        })
                    }
                    
                }
                
                $("#control").html(data.control)   //control按钮名称
                if(data.control == ""){
                    $("#control").remove();
                }
                if(data.control == "取消订单"){
                    $(".order_title2").show();
                }

                //control2投诉按钮名称
                if(data.control2 != ""){
                    var control2 = '<button class="orderdetails_link" id="contro2" style="margin-right:.2rem;">' + data.control2 + '</button>'
                    $("#control").after(control2);
                }
                if(data.status === 5 || data.status === 6){
                    $("#control").css("background","#888");
                }
                if(data.control2 == "订单申诉中"){
                    $("#control").css("background","#888");
                }
                

                // 投诉
                $("#contro2").on("click",function(){
                    layer.alert('是否确认申诉？', {
                        closeBtn: 0    // 是否显示关闭按钮
                        ,anim: 6 //动画类型
                        ,btn: ['确认','取消'] //按钮
                        ,yes:function(){
                            $.ajax({
                                type:"post",
                                url:url+'/user/comsubmit.html?token='+token,
                                async:false,
                                data:{'cs_type':3,"cs_id":id,"describe":describe,"token":token,},
                                dataType:'json',
                                success:function(result){
                                    // console.log(result)
                                    if(result.status === "0001"){
                                        layer.msg(result.message)
                                        window.location.href = "order.html";
                                    }else{
                                        isErr(result)
                                    }
                                },
                                error:function(){
                                    layer.msg("响应失败");
                                }
                            })
                        }
                        ,btn2:function(){

                        }
                    });
                    var describe = "订单号：" + id + "  卖家申诉";
                })

                //倒计时
                $("#timer1").attr("data",data.over_time)
                var addTimer = function () {
                    var list = [],
                    interval;

                    return function (id, timeStamp) {
                        if (!interval) {
                        interval = setInterval(go, 1);
                        }
                        list.push({
                        ele: document.getElementById(id),
                        time: timeStamp
                        });
                    }

                    function go() {
                        for (var i = 0; i < list.length; i++) {
                        list[i].ele.innerHTML = changeTimeStamp(list[i].time);
                        if (!list[i].time)
                            list.splice(i--, 1);
                        }
                    }

                //传入unix时间戳，得到倒计时
                function changeTimeStamp(timeStamp) {
                    var distancetime = new Date(timeStamp * 1000).getTime() - new Date().getTime();
                    if (distancetime > 0) {
                    //如果大于0.说明尚未到达截止时间       
                    var ms = Math.floor(distancetime % 1000);
                    var sec = Math.floor(distancetime / 1000 % 60);
                    var min = Math.floor(distancetime / 1000 / 60 % 60);
                    var hour = Math.floor(distancetime / 1000 / 60 / 60 % 24);
                    var data = Math.floor(distancetime / 1000 / 60 / 60 /24 % 100);

                    if (ms < 100) {
                        ms = "0" + ms;
                    }
                    if (sec < 10) {
                        sec = "0" + sec;
                    }
                    if (min < 10) {
                        min = "0" + min;
                    }
                    if (hour < 10) {
                        hour = "0" + hour;
                    }
                    if (data < 10) {
                        data = "0" + data;
                    }

                    return hour + ":" + min + ":" + sec;
                    }
                    if (timeStamp < Date.parse(new Date())) {
                    //若否，就是已经到截止时间了
                    return "可取消！"
                    }
                }
                }();
                var gh = document.getElementsByClassName("a-join1");
                for (var i = 0; i < gh.length; i++) {
                addTimer(gh[i].id, gh[i].getAttribute("data"));
                }

                // 支付宝二维码
                if(type === 1){
                    $("#order_title").show();
                }
                $("#order_pic").attr("src",data.alipay_img);
                $("#order_pic").on("click",function(){
                    var dia_url2 = $(this).attr("src");
                    $(".dialog_img").attr("src",dia_url2);
                    dialog('#d2');
                })
                dialogClose();
            }else{
                isErr(result)
            }
        }
    })
})

// 按钮判断
function operating(e){
    var id = GetQueryString("id");
    var html = $(e).html();
    var src = $("#headImg").attr("data-url");
    if(html === "确认付款"){
        layer.alert('是否确认付款？', {
            closeBtn: 0    // 是否显示关闭按钮
            ,anim: 6 //动画类型
            ,btn: ['确认','取消'] //按钮
            ,yes:function(){
                $.ajax({
                    type:"post",
                    url:url+'/trade/buyconfirm.html?token='+token,
                    async:false,
                    data:{'token':token,"id":id,"src":src},
                    dataType:'json',
                    success:function(result){
                        // console.log(result)
                        if(result.status === "0001"){
                            layer.msg(result.message)
                            window.location.href = "order.html";
                        }else{
                            isErr(result)
                        }
                    },
                    error:function(){
                        layer.msg("响应失败");
                    }
                })
            }
            ,btn2:function(){}
        });
        
    }else if(html === "确认收款"){
        ajaxd(id,"/trade/sellconfirm.html?token=",html)
    }else if(html === "取消订单"){
        if($("#timer1").html() == "可取消！"){
            ajaxd(id,"/trade/buycancel.html?token=",html)
        }else{
            layer.msg("倒计时结束后才可取消！")
        }
    }
}



// 确认收款、取消订单接口
function ajaxd(id,ajaxd,html) {
    layer.alert('是否' + html + '？', {
        closeBtn: 0    // 是否显示关闭按钮
        ,anim: 6 //动画类型
        ,btn: ['确认','取消'] //按钮
        ,yes:function(){
            $.ajax({
                type:"post",
                url:url + ajaxd + token,
                async:false,
                data:{'token':token,"id":id},
                dataType:'json',
                success:function(result){
                    // console.log(result)
                    if(result.status === "0001"){
                        layer.msg(result.message)
                        window.location.href = "order.html";
                    }else{
                        isErr(result)
                    }
                },
                error:function(){
                    layer.msg("响应失败");
                }
            })
        }
        ,btn2:function(){

        }
    });
    
}


